package com.ecoolex.ying.space.admin.graphql;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import com.google.common.collect.Lists;
import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.OutsourcingGoodsWithBLOBs;
import com.ecoolex.ying.space.entity.OutsourcingPackaged;
import com.ecoolex.ying.space.entity.OutsourcingPackaged.Column;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoods;
import com.ecoolex.ying.space.pojo.AddOutsourcingPackagedDTO;
import com.ecoolex.ying.space.pojo.EditOutsourcingPackagedDTO;
import com.ecoolex.ying.space.ws.OutsourcingGoodsService;

@GraphQL2Module(type = Type.Admin, description = "外购商品套餐管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = OutsourcingPackagedGraphql.AUTH, name = "外购商品套餐管理")
public class OutsourcingPackagedGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-OUTSOURCINGPACKAGED";
	
	@Autowired
	private OutsourcingGoodsService outsourcingGoodsService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = OutsourcingPackaged.class, field = "coverImage", description = "套餐列表图片")
	public StorageInfo coverImage(@Source OutsourcingPackaged source) {
		return storageService.get(source.getCoverImage());
	}
	
	@TypeField(type = OutsourcingPackaged.class, field = "images", description = "套餐图片")
	public List<StorageInfo> images(@Source OutsourcingPackaged source) {
		JSONArray array = JSONArray.parseArray(source.getImages());
		List<String> storageIdLsit = array.toJavaList(String.class);
		return storageIdLsit.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
	}
	
	@TypeField(type = OutsourcingPackaged.class, field = "canOnline", description = "能上线")
	public boolean canOnline(@Source OutsourcingPackaged source) {
		return !source.getOnline();
	}
	
	@TypeField(type = OutsourcingPackaged.class, field = "canOffline", description = "能下线")
	public boolean canOffline(@Source OutsourcingPackaged source) {
		return source.getOnline();
	}
	
	@TypeField(type = OutsourcingPackaged.class, field = "canDel", description = "能删除")
	public boolean canDel(@Source OutsourcingPackaged source) {
		return !source.getOnline();
	}
	
	@TypeField(type = OutsourcingPackaged.class, field = "canEdit", description = "能编辑")
	public boolean canEdit(@Source OutsourcingPackaged source) {
		return !source.getOnline();
	}
	
	@TypeField(type = OutsourcingPackaged.class, field = "outsourcingGoodsList", description = "商品列表")
	public List<OutsourcingGoodsWithBLOBs> outsourcingGoods(@Source OutsourcingPackaged source) {
		List<OutsourcingPackagedGoods> list = outsourcingGoodsService.getOutsourcingPackagedGoodsByOutsourcingPackagedId(source.getOutsourcingPackagedId());
		return list.stream().map(outsourcingPackagedGoods -> outsourcingGoodsService.getOutsourcingGoodsById(outsourcingPackagedGoods.getOutsourcingGoodsId())).collect(Collectors.toList());
	}
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "outsourcingPackaged", description = "外购商品套餐详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "外购商品套餐详情")
	public OutsourcingPackaged get(@NonNull Integer outsourcingPackagedId,AdminMemberAuthority user) {
		user.assertLogin();
		return outsourcingGoodsService.getOutsourcingPackagedById(outsourcingPackagedId);
	}
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "outsourcingPackagedList", description = "外购商品套餐列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "外购商品套餐列表")
	public Pagination<OutsourcingPackaged> query(String name,Boolean online,@NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertLogin();
		return outsourcingGoodsService.queryOutsourcingPackaged(name,online, sorts, page);
	}
	
	@MutationField(field = "uploadOutsourcingPackagedCoverImage", description = { "上传套餐列表图片" })
	public StorageInfo uploadOutsourcingPackagedCoverImage(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
			user.notAuthority();
		}
		UploadSource uploadSource = new UploadSource("OutsourcingPackaged","CoverImage");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());
        
        return storageService.upload(request,uploadSource,uploadLimit);
	}
	
	@MutationField(field = "uploadOutsourcingPackagedImages", description = { "上传套餐图片" })
	public StorageInfo uploadOutsourcingPackagedImages(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
			user.notAuthority();
		}
		UploadSource uploadSource = new UploadSource("OutsourcingPackaged","Images");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());
        
        return storageService.upload(request,uploadSource,uploadLimit);
	}
	
	@MutationField(field = "uploadOutsourcingPackagedDetail", description = { "上传套餐详情" })
	public StorageInfo uploadOutsourcingPackagedDetail(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
			user.notAuthority();
		}
		UploadSource uploadSource = new UploadSource("OutsourcingPackaged","Detail");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());
        
        return storageService.upload(request,uploadSource,uploadLimit);
	}
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addOutsourcingPackaged", description = "添加外购商品套餐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加外购商品套餐")
	public OutsourcingPackaged addOutsourcingPackaged(@NonNull AddOutsourcingPackagedDTO add,@NonNull List<Integer> outsourcingGoodsIds,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return outsourcingGoodsService.addOutsourcingPackaged(add,outsourcingGoodsIds, user.userName());
	}
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editOutsourcingPackaged", description = "编辑外购商品套餐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑外购商品套餐")
	public OutsourcingPackaged editOutsourcingPackaged(@NonNull EditOutsourcingPackagedDTO edit,@NonNull List<Integer> outsourcingGoodsIds,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		OutsourcingPackaged old = outsourcingGoodsService.getOutsourcingPackagedById(edit.getOutsourcingPackagedId());
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商品套餐不存在");
		
		BeanUtil.copy(edit,
				old,
				Lists.newArrayList(Column.online, Column.deleted, Column.createUser, Column.createTime).stream().map(
					Column::getJavaProperty).toArray(String[]::new));
		return outsourcingGoodsService.editOutsourcingPackaged(old,outsourcingGoodsIds,user.userName());
	}
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "deleteOutsourcingPackaged", description = "删除外购商品套餐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除外购商品套餐")
	public boolean deleteOutsourcingPackaged(@NonNull Integer outsourcingPackagedId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		OutsourcingPackaged old = outsourcingGoodsService.getOutsourcingPackagedById(outsourcingPackagedId);
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商品套餐不存在");

		outsourcingGoodsService.deleteOutsourcingPackaged(old, user.userName());
		return true;
	}
	
	private static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	@MutationField(field = "onlineOutsourcingPackaged", description = "上线外购商品套餐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线外购商品套餐")
	public boolean onlineOutsourcingPackaged(@NonNull Integer outsourcingPackagedId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		OutsourcingPackaged old = outsourcingGoodsService.getOutsourcingPackagedById(outsourcingPackagedId);
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商品套餐不存在");
		
		outsourcingGoodsService.onlineOutsourcingPackaged(old, user.userName());
		return true;
	}
	
	private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	@MutationField(field = "offlineOutsourcingPackaged", description = "下线外购商品套餐")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线外购商品套餐")
	public boolean offlineOutsourcingPackaged(@NonNull Integer outsourcingPackagedId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		OutsourcingPackaged old = outsourcingGoodsService.getOutsourcingPackagedById(outsourcingPackagedId);
		Assert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(1), "商品套餐不存在");
		
		outsourcingGoodsService.offlineOutsourcingPackaged(old, user.userName());
		return true;
	}
}
